import DataManager  from './DataManager';
import { nanoid } from "nanoid";
export default class Node {
    constructor(node) {
      this.dm = DataManager.instance();      
      this.transform = node.transform || { x: 0, y: 0, w: 0, h: 0 };
      this.scale = node.scale || { x: 1, y: 1 };
      this.property = node.property || { lineWidth: 1 };
      this.order =0
      this.layer=node.layer||"base"
      this.name = node.name||'node'
      this.id=node.id||''
      this.uid =nanoid()
    
    }
    render(context) {
      
      if(this.uid===this.dm.UIslectNode.selected.uid){
        this.showUISelect();
      }
    }
    clear(context) {
      let transformScale = {
        x: this.transform.x,
        y: this.transform.y,
        w: this.transform.w * this.scale.x,
        h: this.transform.h * this.scale.y,
      };
  
      context.clearRect(
        this.transform.x * this.scale.x,
        this.transform.y * this.scale.y,
        this.transform.w * this.scale.x,
        this.transform.h * this.scale.y
      );
  
    }
    checkRect(pos){
      let trans ={
        x:this.transform.x*this.dm.worldScale.scale,
        y:this.transform.y*this.dm.worldScale.scale,
        w:this.transform.w * this.scale.x *this.dm.worldScale.scale,
        h:this.transform.h * this.scale.y *this.dm.worldScale.scale
      }

      trans.x1 = trans.x+trans.w
      trans.y1 = trans.y+trans.h
      

      if(trans.x<=pos.x&&trans.y<=pos.y&&pos.x<=trans.x1&&pos.y<=trans.y1){
        return true
      }
      return false
  

    }

    showUISelect(){
      let trans ={
        x:this.transform.x*this.dm.worldScale.scale+this.dm.worldTransForm.x,
        y:this.transform.y*this.dm.worldScale.scale+this.dm.worldTransForm.y,
        w:this.transform.w * this.scale.x *this.dm.worldScale.scale,
        h:this.transform.h * this.scale.y *this.dm.worldScale.scale
      }
      this.dm.UIslectNode.node.hidden=false;
      this.dm.UIslectNode.node.style=`top:${trans.y}px;left:${trans.x}px;width:${trans.w}px;height:${trans.h}px;`
      this.dm.UIslectNode.selected = this;
      // this.isSelected=true;
    }
    hideUISelect(){
      this.dm.UIslectNode.node.hidden=true;
      this.dm.UIslectNode.selected = {};
    }
    toObj(){
      return {
        id:this.id,
    
        transform : this.transform,
        scale : this.scale,
        property : this.property,
        order :this.order,
        layer:this.layer,
        name : this.name,
        uid :this.uid
      }
    }
    
  }
  